package com.dormitory.dormitorymanagement.controller;

import com.dormitory.dormitorymanagement.constant.Constant;
import com.dormitory.dormitorymanagement.mapper.StudentsMapper;
import com.dormitory.dormitorymanagement.pojo.*;
import com.dormitory.dormitorymanagement.pojo.dto.LoginDto;
import com.dormitory.dormitorymanagement.pojo.dto.UserDto;
import com.dormitory.dormitorymanagement.pojo.vo.LoginVo;
import com.dormitory.dormitorymanagement.service.AdminService;
import com.dormitory.dormitorymanagement.service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
@CrossOrigin(origins = "http://localhost:8081", allowedHeaders = "*")
public class AuthController {


    //软件2201 刘丽鹃
    @Autowired
    private StudentsService studentsService;

    @Autowired
    private StudentsMapper studentsMapper;

    @Autowired
    private AdminService adminService;


    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = new LoginVo();
        if (loginDto.getRole().equals("student")) {
            Students students = studentsService.login(loginDto);
            if (students == null) {
                return Result.error(Constant.USER_UNEXIST);
            } else {
                if (!students.getPassword().equals(loginDto.getPassword())) {
                    return Result.error(Constant.PASSWORD_ERROR);
                }

                loginVo.setStudentId( students.getStudentId());
                loginVo.setName( students.getName());
                loginVo.setRole( students.getRole());
                loginVo.setUsername( students.getUsername());
            }

        }
        if (loginDto.getRole().equals("admin")) {

            DormitoryAdmin dormitoryAdmin = adminService.login(loginDto);
            if (dormitoryAdmin == null) {
                return Result.error(Constant.USER_UNEXIST);
            }
            if (!dormitoryAdmin.getPassword().equals(loginDto.getPassword())) {
                return Result.error(Constant.PASSWORD_ERROR);
            }
            loginVo.setStudentId(dormitoryAdmin.getStaffId());
            loginVo.setRole("admin");
             loginVo.setName(dormitoryAdmin.getName());
             loginVo.setUsername( dormitoryAdmin.getUsername());
             loginVo.setDormBuilding(dormitoryAdmin.getBuildingResponsible());
        }

        if(loginDto.getRole().equals("classLeader")){
            Students students = studentsService.login(loginDto);
            if (students == null || students.getRole().equals("学生")) {
                return Result.error(Constant.USER_UNEXIST);
            } else {
                if (!students.getPassword().equals(loginDto.getPassword())) {
                    return Result.error(Constant.PASSWORD_ERROR);
                }
                loginVo.setStudentId( students.getStudentId());
                loginVo.setName( students.getName());
                loginVo.setRole( students.getRole());
                loginVo.setUsername( students.getUsername());
            }
        }
        return  Result.success(loginVo);
    }



    @PostMapping("/register")
    public Result register(@RequestBody UserDto userDto) {
        if (userDto.getRole().equals("student")){
            Students byStudentId = studentsMapper.findByStudentId(userDto.getStudentId());
            if (byStudentId != null){
                return Result.error("该学号已注册");
            }
            studentsService.studentRedister(userDto);
        }
        if (userDto.getRole().equals("admin")){
            adminService.adminRedister(userDto);
        }
        if(userDto.getRole().equals("classLeader")){
            Students byStudentId = studentsMapper.findByStudentId(userDto.getStudentId());
            if (byStudentId != null){
                return Result.error("该学号已注册");
            }
            studentsService.classLeaderRedister(userDto);
        }
        return Result.success();
    }
}
